package de.blitzcoder.collide.templatemanager;

import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.LinkedList;
import javax.swing.KeyStroke;

/**
 *
 * @author blitzcoder
 */
public class Template implements Serializable {

    // List of all templates
    private static LinkedList<Template> list = new LinkedList<Template>();
    
    // File to store templates
    private static final String templateFile = "templates.obj";

    // Properties
    private String name;
    private KeyStroke shortcut;
    private String shortcutText;
    private String code;
    private LinkedList<TemplateVariable> variables;

    public Template() {
        name = "Neues Template";
        shortcut = KeyStroke.getKeyStroke(KeyEvent.VK_F1,KeyEvent.ALT_MASK|KeyEvent.CTRL_MASK);
        variables = new LinkedList<TemplateVariable>();
        shortcutText = "";
        code = "'Code";
        list.addLast(this);
    }

    public static LinkedList<Template> getList() {
        return list;
    }

    public void addVariable(TemplateVariable var) {
        variables.addLast(var);
    }
    
    public LinkedList<TemplateVariable> getVariables() {
        return variables;
    }
    
    public void removeVariable(int index) {
        variables.remove(index);
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setShortcut(KeyStroke shortcut) {
        this.shortcut = shortcut;
    }

    public KeyStroke getShortcut() {
        return shortcut;
    }

    public void setShortcutText(String shortcutText) {
        this.shortcutText = shortcutText;
    }

    public String getShortcutText() {
        return shortcutText;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getCode() {
        return code;
    }

    @Override
    public String toString() {
        return getName();
    }

    /*
     * Load the templates from a file
     */
    public static void loadTemplates() {

        File file = new File(templateFile);
        if (file.exists()) {
            try {
                ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
                Object obj = in.readObject();
                list = (LinkedList<Template>)obj;
                in.close();
            } catch (Exception ex) {
                throw new Error(ex);
            }
        }

        TemplateManager.fillTemplateMenu();

    }

    /*
     * Save the templates
     */
    public static void saveTemplates() {
        
        File file = new File(templateFile);
        
        try {
            
            if (file.exists()) {
                file.delete();
                file.createNewFile();
            }
            
            ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file));
            out.writeObject(list);
            out.close();
        } catch (Exception ex) {
            throw new Error(ex);
        }

    }


}
